package cn.zurish.cloud.security.controller;

import cn.zurish.cloud.security.dto.AuthResponse;
import cn.zurish.cloud.security.toolkit.JwtUtils;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class AuthController {

    private final JwtUtils jwtUtils;

    // 简单示例，真实场景中应从数据库加载用户信息并校验密码
    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody LoginRequest loginRequest) {
        // 这里假设用户名为 "admin"，密码为 "admin"
        if ("admin".equals(loginRequest.getUsername()) && "admin".equals(loginRequest.getPassword())) {
            String token = jwtUtils.generateToken(loginRequest.getUsername());
            return ResponseEntity.ok(token);
        }
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户名或密码错误");
    }

    // 简单示例，真实场景中应从数据库加载用户信息并校验密码
    @GetMapping("/verify")
    public ResponseEntity<?> verify() {
        return ResponseEntity.ok("验证用户访问成功");
    }

    //通过数据库用户数据登陆
    @PostMapping("/loginByMysql")
    public AjaxResult<AuthResponse> loginByMysql(@RequestBody LoginRequest request) {
        return AjaxResult.success(authService.authenticate(request));
    }

    //验证权限
    @PreAuthorize("hasAuthority('admin:menu:add')")
    @GetMapping("/add")
    public AjaxResult<Void> add() {
        return AjaxResult.success("方法的授权admin:menu:add,访问ok");
    }

}
